﻿using BP.Plugin.Monitor.Application.Common.Interfaces;
using BP.Plugin.Monitor.Domain.Entities;
using Core.Persistence;
using Finbuckle.MultiTenant.Abstractions;
using Infrastructure.Persistence;
using Infrastructure.Tenant;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;

namespace BP.Plugin.Monitor.Infrastructure.Persistence;

public sealed class MonitorDbContext : BoilerplateDbContext, IMonitorDbContext
{
    public const string SchemaName = SchemaNames.Monitor;
    private readonly IOptions<DatabaseOptions> _settings;

    public MonitorDbContext(
        IMultiTenantContextAccessor<BoilerplateTenantInfo> multiTenantContextAccessor,
        DbContextOptions<MonitorDbContext> options,
        IPublisher publisher,
        IOptions<DatabaseOptions> settings)
        : base(multiTenantContextAccessor, options, publisher, settings)
    {
        _settings = settings;
    }

    public DbSet<OnlineUser> OnlineUsers => Set<OnlineUser>();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        ArgumentNullException.ThrowIfNull(modelBuilder);
        base.OnModelCreating(modelBuilder);
        modelBuilder.ApplyConfigurationsFromAssembly(typeof(MonitorDbContext).Assembly);
        modelBuilder.HasDefaultSchema(SchemaName);
        modelBuilder.ConfigureDateTimeForPostgreSQL(_settings.Value);
    }
}
